package persistence;

import java.util.ArrayList;
import java.util.List;

import entity.Funcionario;

public class FuncionarioDao extends Dao{

	
	public Integer create(Funcionario f) throws Exception{
		open();
			stmt = con.prepareStatement("insert into funcionario values(seq_funcionario.nextval,?,?)");
				stmt.setString(1, f.getNome());
				stmt.setDouble(2, f.getSalario());
				stmt.execute();
				
			stmt = con.prepareStatement("select seq_funcionario.currval from dual");
			rs = stmt.executeQuery();
			rs.next();
				int chave = rs.getInt(1);
			stmt.close();
		close();
		return chave;
	}
	
	public List<Funcionario> findAll() throws Exception{
		open();
			stmt = con.prepareStatement("select * from funcionario");
			List<Funcionario> lista = new ArrayList<Funcionario>();
			rs = stmt.executeQuery();
			
			while(rs.next()){
				Funcionario f = new Funcionario(rs.getInt(1), 
						                       rs.getString(2), 
						                       rs.getDouble(3));
				lista.add(f);
			}
		close();
		return lista;
	}
	
	public Funcionario findByCode(Integer cod) throws Exception{
		open();
			stmt = con.prepareStatement("select * from funcionario where idFuncionario=?");
				stmt.setInt(1, cod);
			rs = stmt.executeQuery();
			
			Funcionario f = null;
			if(rs.next()){
				f = new Funcionario(rs.getInt(1), 
						                       rs.getString(2), 
						                       rs.getDouble(3));
			}
		close();
		return f;
	}
	
	
	
}
